package app.Servidor;

/**
 *
 * @author Pedro Santos
 */
import java.io.IOException;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextArea;

public class ServerUdp extends Thread {
    /*
     * O servidor Usa a porta 8080 para escutar.
     */

    Thread thread;
    DatagramSocket socket;
    int port;
    Servidor servidor;
    InetAddress client; // Endereço do Cliente.
    int client_port; // Port do cliente.    

    public ServerUdp(Servidor servidor) {
        int port = 8070;
        this.servidor = servidor;
    }

    public ServerUdp(Servidor servidor, int port) {
        this.port = port;
        //System.out.println("Porta" + port);
        this.servidor = servidor;
    }

    @Override
    public void run() {
        try {
            byte[] buffer = new byte[1024];//Tamanho máximo da mensagem.
            int port = this.port;
            try {
                socket = new DatagramSocket(port);
                while (true) {
                    try {
                       // System.out.println("Server Iniciado!");
                        //Receive request from client
                        DatagramPacket packet =
                                new DatagramPacket(buffer, buffer.length);
                       // System.out.println("Aguardando Requisição...");
                        socket.receive(packet);
                       // System.out.println("Chegou");
                        client = packet.getAddress();
                        client_port = packet.getPort();
                    } catch (UnknownHostException ue) {
                    }

                    //-------Resposta---------------------
                    String capitalizedSentence = "OK";
                    byte[] sendData = capitalizedSentence.getBytes();

                    DatagramPacket sendPacket =
                            new DatagramPacket(sendData, sendData.length, client, client_port);

                    try {
                        socket.send(sendPacket);

                    } catch (IOException ex) {
                        // Logger.getLogger(Ouvinte.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    Decodificador deco = new Decodificador(servidor);
                    deco.Decodificar(buffer, client);
                }
            } catch (java.net.BindException b) {
            }
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public void off() {
        thread.interrupted();
        socket.close();
    }

    public InetAddress getClient() {
        return client;
    }

    public void setClient(InetAddress client) {
        this.client = client;
    }

    public int getClient_port() {
        return client_port;
    }

    public void setClient_port(int client_port) {
        this.client_port = client_port;
    }
}
